package edu.uc.ui.ctrl25;

import java.util.List;

import org.apache.log4j.net.SyslogAppender;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import com.liuvei.common.PagerItem;
import com.liuvei.common.SysFun;

import edu.uc.bean.NewsCat;
import edu.uc.service.NewsCatService;

@Repository("NewsCatAction")
@Scope("prototype")
public class NewsCatAction extends BaseAction {
	private static final long serialVersionUID = 1L;

	@Autowired
	private NewsCatService newsCatService;

	private String id;
	private String catId;
	private String catName;
	private String catDesc;

	public String list() {
		List<NewsCat> vList = null;

		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);

		Long rowCount = newsCatService.count();

		pagerItem.changeRowCount(rowCount);

		vList = newsCatService.pager(pagerItem.getPageNum(), pagerItem.getPageSize());

		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));

		request.put("pagerItem", pagerItem);
		request.put("dataList", vList);
		return "list";
	}

	public String listDeal() {
		request.put("catName", catName);
		if (!SysFun.isNullOrEmpty(catName)) {
			catName = catName.trim();
		} else {
			catName = "";
		}
		System.out.println(catName);
		List<NewsCat> vList = null;

		PagerItem pagerItem = new PagerItem();
		pagerItem.parsePageSize(pageSize);
		pagerItem.parsePageNum(pageNum);

		Long rowCount = newsCatService.countByName(catName);

		pagerItem.changeRowCount(rowCount);

		vList = newsCatService.pagerByName(catName, pagerItem.getPageNum(), pagerItem.getPageSize());

		pagerItem.changeUrl(SysFun.generalUrl(requestURI, queryString));

		request.put("pagerItem", pagerItem);
		request.put("dataList", vList);
		return "list";
	}

	public String insert() {
		return "insert";
	}

	public String insertDeal() {
		request.put("catName", catName);
		request.put("catDesc", catDesc);
		String vMsg = "";
		if (SysFun.isNullOrEmpty(catName)) {
			vMsg += "类名不能为空";
		}
		if (SysFun.isNullOrEmpty(catDesc)) {
			vMsg += "描述不能为空";
		}

		if (!SysFun.isNullOrEmpty(vMsg)) {
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "insert";
		}
		NewsCat bean = new NewsCat();
		bean.setCatName(catName);
		bean.setCatDesc(catDesc);
		bean.setParentId(0L);
		Long result = 0L;
		try {
			result = newsCatService.insert(bean);
		} catch (Exception e) {
			// TODO: handle exception
			vMsg += "新建失败" + e.getMessage();
		}
		if (result > 0) {
			return "go_preload";
		} else {
			request.put("msg", vMsg);
			return "insert";
		}
	}

	public String delete() {
		return "go_ok";
	}

	public String deleteDeal() {
		if (!SysFun.isNullOrEmpty(id)) {
			Long iId = SysFun.parseLong(id);
			Long result = newsCatService.delete(iId);
			if (result > 0) {
				return "go_ok";
			}

		}
		return "go_no";
	}

	public String update() {
		if (!SysFun.isNullOrEmpty(id)) {
			Long iId = SysFun.parseLong(id);
			NewsCat bean = newsCatService.load(iId);
			if (bean != null) {
				request.put("catId", bean.getCatId());
				request.put("catName", bean.getCatName());
				request.put("catDesc", bean.getCatDesc());
				return "update";
			}
		}

		return "update";
	}

	public String updateDeal() {
		Long result = 0L;
		String vMsg = "";
		request.put("catName", catName);
		request.put("catDesc", catDesc);
		request.put("catId", catId);
		if (SysFun.isNullOrEmpty(catName)) {
			vMsg += "类名不能为空";
		}
		if (SysFun.isNullOrEmpty(catDesc)) {
			vMsg += "描述不能为空";
		}
		if (SysFun.isNullOrEmpty(catId)) {
			vMsg += "Id不能为空";
		}

		if (!SysFun.isNullOrEmpty(vMsg)) {
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		Long iId = SysFun.parseLong(catId);
		NewsCat bean = newsCatService.load(iId);
		if (bean == null) {
			vMsg += "��ѯ����������¼";
		}
		if (!SysFun.isNullOrEmpty(vMsg)) {
			request.put("msg", vMsg);
			System.out.println(vMsg);
			return "update";
		}
		bean.setCatName(catName);
		bean.setCatDesc(catDesc);

		System.out.println(bean.getCatDesc() + "111111");
		System.out.println(bean.getCatName() + "222222");
		System.out.println(bean.getCatId() + "33333");
		try {
			result = newsCatService.update(bean);
		} catch (Exception e) {
			// TODO: handle exception
			vMsg += "更新失败" + e.getMessage();
		}
		if (result > 0) {
			return "go_preload";
		} else {
			request.put("msg", vMsg);
			return "update";
		}
	}

	public String detail() {
		if (!SysFun.isNullOrEmpty(id)) {
			String vMsg = "";
			Long iId = SysFun.parseLong(id);
			NewsCat bean = newsCatService.load(iId);

			if (bean != null) {
				request.put("catId", bean.getCatId());
				request.put("catName", bean.getCatName());
				request.put("catDesc", bean.getCatDesc());
				return "detail";
			} else {
				vMsg += "没有找到该数据";
			}
		}
		return "detail";

	}

	public String getCatName() {
		return catName;
	}

	public void setCatName(String catName) {
		this.catName = catName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCatId() {
		return catId;
	}

	public void setCatId(String catId) {
		this.catId = catId;
	}

	public String getCatDesc() {
		return catDesc;
	}

	public void setCatDesc(String catDesc) {
		this.catDesc = catDesc;
	}

}
